<template>
  <view :class="[ns.b(), ns.m(modelData.id), ...controller.containerClass]">
    <i-biz-view-shell
      v-if="isLoaded"
      :context="viewData.context"
      :params="viewData.params"
      :viewId="viewData.viewConfig.id"
      :key="viewData.viewConfig.codeName"
      :state="{ srfnav: viewData.srfnav }"
    ></i-biz-view-shell>
  </view>
</template>
<script lang="ts">
export default {
  options: {
    addGlobalClass: true,
    virtualHost: true,
    styleIsolation: "shared"
  }
}
</script>
<script setup lang="ts">
import { PropType, ref, toRaw, onMounted, onUnmounted } from "vue";
import { IPanelRawItem } from "@ibiz/model-core";
import { NavPosIndexController } from "./i-biz-nav-pos-index.controller";
import { useNamespace } from "@/use";
import { parsePath, parseRouteViewData } from "@/utils";
import { IRouteViewData } from "@/interface";
import { IBizContext } from "@ibiz-template/core";

const ns = useNamespace("nav-pos-index");

const props = defineProps({
  modelData: {
    type: Object as PropType<IPanelRawItem>,
    required: true,
  },
  controller: {
    type: NavPosIndexController,
    required: true,
  },
});

const c = props.controller;

const isLoaded = ref(false);

const viewData = ref<IRouteViewData>({});

/**
 * 初始化视图数据
 */
const initViewData = async (arg: IData): Promise<void> => {
  isLoaded.value = false;
  try {
    const { fullPath } = arg;
    const route = parsePath(fullPath);
    const _viewData = await parseRouteViewData(route);
    viewData.value = {
      ..._viewData,
      context: IBizContext.create(_viewData.context),
    };
  } catch (error) {
  } finally {
    isLoaded.value = true;
  }
};

/**
 * 渲染完成
 */
onMounted(() => {
  uni.$on("openView", initViewData);
});

/**
 * 页面卸载
 */
onUnmounted(() => {
  uni.$off("openView", initViewData);
});
</script>
<style lang="scss">
@import "./i-biz-nav-pos-index.scss";
</style>
